﻿/*******************************************************************************
* File Name  : NFPlayerBase.cpp
* Description: 
* Author     : gaoyi_ds
* Time       : 2024-07-29
* Version    : 1.0
* History:
* <author>      <time>      <version >  <desc>
*******************************************************************************/

#include "NFPlayerBase.h"

#include "proto_db.pb.h"
#include "proto_event.nanopb.h"
#include "proto_event.pb.h"
#include "DescStore/ActorDesc.h"
#include "NFComm/NFCore/NFServerTime.h"
#include "NFComm/NFPluginModule/NFCheck.h"
#include "NFComm/NFPluginModule/NFIEventModule.h"

NFPlayerBase::NFPlayerBase()
{
    if (EN_OBJ_MODE_INIT == NFShmMgr::Instance()->GetCreateMode())
    {
        CreateInit();
    }
    else
    {
        ResumeInit();
    }
}

NFPlayerBase::~NFPlayerBase()
{
}

int NFPlayerBase::CreateInit()
{
    m_bIsInited = false;
    m_cs_seq = 0;
    m_iTransNum = 0;
    m_lastUpdateExpTime = 0;
    m_lastUpdateGSTime = 0;
    m_lastUpdateAheadCostTime = 0;
    m_lastSycPlatDataTime = 0;
    m_needUpdateExp = true;
    m_needUpdateGS = true;
    return 0;
}

int NFPlayerBase::ResumeInit()
{
    return 0;
}

int NFPlayerBase::Init()
{
    return 0;
}

int NFPlayerBase::UnInit()
{
    return 0;
}

int NFPlayerBase::LoadFromDB(const proto_ff::tbRoleDetail* pDbData, const RoleOne& stRoleOne)
{
    if (pDbData)
    {
        m_stRoleBase.FromPb(pDbData->role_base_data().base());
    }
    m_stRoleCache = stRoleOne;
    m_bIsInited = true;
    ResetCsMsgSeq();
    return 0;
}

int NFPlayerBase::InitConfig(proto_ff::tbRoleDetail* pDbData, const RoleOne& stRoleOne)
{
    CHECK_NULL(0, pDbData);
    pDbData->set_role_id(stRoleOne.role_id);
    auto pRoleBase = pDbData->mutable_role_base_data()->mutable_base();

    auto pActorDesc = ActorDesc::Instance()->GetDesc(stRoleOne.actor_id);
    CHECK_NULL(0, pActorDesc);

    pRoleBase->set_profession((proto_ff::enProfessionType)pActorDesc->job);
    pRoleBase->set_role_id(stRoleOne.role_id);
    pRoleBase->set_name(stRoleOne.name.data());
    pRoleBase->set_level(1);
    pRoleBase->set_exp(0);
    pRoleBase->set_rank(1250);
    pRoleBase->set_division(0);
    pRoleBase->set_avatar_id(1);
    pRoleBase->set_create_time(0);
    pRoleBase->set_last_login_time(NF_ADJUST_TIMENOW());
    pRoleBase->set_last_logout_time(0);
    pRoleBase->set_login_state(proto_ff::E_CREATE_WAIT_NAME);

    proto_ff::Proto_IdTypeItem* pItem = pRoleBase->mutable_logo_id();
    pItem->set_id(0);
    pItem->set_type(0);
    proto_ff::Proto_RoleTimeValue* pTimeValue = pRoleBase->mutable_time_prop();
    pTimeValue->set_value(0);
    pTimeValue->set_next_add_value_time(NF_ADJUST_TIMENOW());
    proto_ff::Birthday* pBirthday = pRoleBase->mutable_birthday();
    pBirthday->set_year(1970);
    pBirthday->set_month(1);
    pBirthday->set_day(1);

    pRoleBase->set_is_public_base(true);
    pRoleBase->set_is_public_sns(true);
    pRoleBase->set_is_public_equip(true);
    pRoleBase->set_is_public_partner(true);
    pRoleBase->set_is_public_rune(true);
    pRoleBase->set_phone_num(0);
    pRoleBase->set_first_reg_time(0);
    return 0;
}

int NFPlayerBase::SaveDB(proto_ff::tbRoleDetail* pDbData)
{
    if (pDbData)
    {
        pDbData->set_role_id(m_stRoleBase.role_id);
        m_stRoleBase.ToPb(pDbData->mutable_role_base_data()->mutable_base());
    }
    return 0;
}

int NFPlayerBase::SaveRoleSimpleInfo(proto_ff::Proto_RoleSimpleInfo* pDbData)
{
    CHECK_NULL(0, pDbData);
    pDbData->set_role_id(m_stRoleBase.role_id);
    pDbData->set_role_id(m_stRoleBase.role_id);
    pDbData->set_name(m_stRoleBase.name.c_str());
    pDbData->set_level(m_stRoleBase.level);
    pDbData->set_ce(m_stRoleBase.role_ce);
    pDbData->set_avatar_id(m_stRoleBase.avatar_id);
    pDbData->set_last_logout_time(0);
    pDbData->set_signature(m_stRoleBase.signature.c_str());
    m_stRoleBase.birthday.ToPb(pDbData->mutable_birthday());
    pDbData->set_zodiac((proto_ff::enPlayerZodiac)m_stRoleBase.zodiac);
    pDbData->set_sex((proto_ff::enSexType)m_stRoleBase.sex);
    pDbData->set_location(m_stRoleBase.location.c_str());
    pDbData->set_profession((proto_ff::enProfessionType)m_stRoleBase.profession);

    pDbData->set_hunter_prof((proto_ff::ENHunterProfType)m_stRoleBase.hunter_prof);
    pDbData->set_create_time(m_stRoleBase.create_time);
    pDbData->set_be_used_name(m_stRoleBase.be_used_name.c_str());
    pDbData->set_open_id(m_stRoleBase.open_id.c_str());
    return 0;
}

int NFPlayerBase::LogicTick()
{
    return 0;
}

int NFPlayerBase::OnRegister()
{
    return 0;
}

int NFPlayerBase::OnLogin()
{
    return 0;
}

int NFPlayerBase::OnLogout()
{
    return 0;
}

int NFPlayerBase::OnDisconnect()
{
    return 0;
}

int NFPlayerBase::OnReconnect()
{
    return 0;
}

int NFPlayerBase::DayTick()
{
    return 0;
}

int NFPlayerBase::DayOtherTick()
{
    return 0;
}

int NFPlayerBase::WeekTick()
{
    return 0;
}

int NFPlayerBase::MonthTick()
{
    return 0;
}

int NFPlayerBase::SecTick()
{
    return 0;
}

int NFPlayerBase::FiveSecTick()
{
    return 0;
}

int NFPlayerBase::MinuteTick()
{
    return 0;
}

int NFPlayerBase::FiveMinuteTick()
{
    return 0;
}

int NFPlayerBase::HourTick()
{
    return 0;
}

void NFPlayerBase::SetRoleName(const std::string& name)
{
    m_stRoleBase.name = name;
    MarkDirty();
}

void NFPlayerBase::SetLastLoginTime(uint32_t loginTime)
{
    m_stRoleBase.last_login_time = loginTime;
    MarkDirty();
}

void NFPlayerBase::SetBirthday(const proto_ff::Birthday& birthday)
{
    m_stRoleBase.birthday.year = birthday.year();
    m_stRoleBase.birthday.month = birthday.month();
    m_stRoleBase.birthday.day = birthday.day();
    MarkDirty();
}

void NFPlayerBase::IncreaseTransNum()
{
    ++m_iTransNum;
}

void NFPlayerBase::DecreaseTransNum()
{
    --m_iTransNum;
    if (m_iTransNum < 0)
    {
        m_iTransNum = 0;
    }
}



int NFPlayerBase::MakeRoleInitData(proto_ff::RoleBase* pRoleBase, uint64_t ullRoleId, const char* pszName, int iActorId)
{
    auto pActorDesc = ActorDesc::Instance()->GetDesc(iActorId);
    CHECK_EXPR(pActorDesc, -1, "iActorId:{}", iActorId);
    pRoleBase->set_profession((proto_ff::enProfessionType)pActorDesc->job);
    pRoleBase->set_role_id(ullRoleId);
    pRoleBase->set_name(pszName);
    pRoleBase->set_world_id(NFGlobalSystem::Instance()->GetGlobalPluginManager()->GetZoneID());
    pRoleBase->set_level(1);
    pRoleBase->set_exp(0);
    pRoleBase->set_rank(1250);
    pRoleBase->set_division(0);
    pRoleBase->set_avatar_id(1);
    pRoleBase->set_create_time(0);
    pRoleBase->set_last_login_time(NF_ADJUST_TIMENOW());
    pRoleBase->set_last_logout_time(0);
    pRoleBase->set_login_state(proto_ff::E_CREATE_WAIT_NAME);

    proto_ff::Proto_IdTypeItem* pItem = pRoleBase->mutable_logo_id();
    pItem->set_id(0);
    pItem->set_type(0);
    proto_ff::Proto_RoleTimeValue* pTimeValue = pRoleBase->mutable_time_prop();
    pTimeValue->set_value(0);
    pTimeValue->set_next_add_value_time(NF_ADJUST_TIMENOW());
    proto_ff::Birthday* pBirthday = pRoleBase->mutable_birthday();
    pBirthday->set_year(1970);
    pBirthday->set_month(1);
    pBirthday->set_day(1);

    pRoleBase->set_is_public_base(true);
    pRoleBase->set_is_public_sns(true);
    pRoleBase->set_is_public_equip(true);
    pRoleBase->set_is_public_partner(true);
    pRoleBase->set_is_public_rune(true);
    pRoleBase->set_phone_num(0);
    pRoleBase->set_first_reg_time(0);
    return 0;
}

void NFPlayerBase::ResetCsMsgSeq()
{
    m_cs_seq = 0;
    MarkDirty();
}

void NFPlayerBase::IncCsMsgSeq()
{
    m_cs_seq++;
    MarkDirty();
}

uint16_t NFPlayerBase::GetCsMsgSeq()
{
    return m_cs_seq;
}

void NFPlayerBase::SetLastLogoutTime(uint32_t logoutTime)
{
    m_stRoleBase.last_logout_time = logoutTime;
    m_stRoleBase.today_online_time += logoutTime - m_stRoleBase.last_login_time;
    MarkDirty();
}

void NFPlayerBase::SetCreateTime(uint32_t createTime)
{
    m_stRoleBase.create_time = createTime;
    MarkDirty();
}

void NFPlayerBase::SetAutoFight(bool bAutoFight)
{
    m_stRoleBase.auto_fight = bAutoFight;
    MarkDirty();
}

void NFPlayerBase::SetIsPublicBase(bool is_public_base)
{
    m_stRoleBase.is_public_base = is_public_base;
    MarkDirty();
}

void NFPlayerBase::SetIsPublicSns(bool is_public_sns)
{
    m_stRoleBase.is_public_sns = is_public_sns;
    MarkDirty();
}

void NFPlayerBase::SetIsPublicEquip(bool is_public_equip)
{
    m_stRoleBase.is_public_equip = is_public_equip;
    MarkDirty();
}

void NFPlayerBase::SetIsPublicPartner(bool is_public_partner)
{
    m_stRoleBase.is_public_partner = is_public_partner;
    MarkDirty();
}

void NFPlayerBase::SetIsPublicRune(bool is_public_rune)
{
    m_stRoleBase.is_public_rune = is_public_rune;
    MarkDirty();
}

void NFPlayerBase::SetPhoneNum(int64_t phone_num)
{
    m_stRoleBase.phone_num = phone_num;
    MarkDirty();
}

void NFPlayerBase::SetOpenID(const std::string& open_id)
{
    m_stRoleBase.open_id = open_id;
    MarkDirty();
}

void NFPlayerBase::SetHunterProf(ENHunterProfType type)
{
    m_stRoleBase.hunter_prof = type;
    MarkDirty();
}

void NFPlayerBase::SetHallInvited(bool bIsHallInvited)
{
    m_stRoleBase.is_hall_invited = bIsHallInvited;
    MarkDirty();
}

void NFPlayerBase::SetPlatType(const Proto_PlatType plat_type)
{
    m_stRoleBase.plat_type = plat_type;
    MarkDirty();
}

void NFPlayerBase::SetSex(enSexType type)
{
    m_stRoleBase.sex = type;
    MarkDirty();
}

void NFPlayerBase::SetProfession(enProfessionType type)
{
    m_stRoleBase.profession = type;
    MarkDirty();
}

void NFPlayerBase::SetExp(uint64_t ullExp)
{
    m_stRoleBase.exp = ullExp;
    _has_bits_.SetBit(proto_ff::EN_ROLE_ATTR_EXP);
    MarkDirty();
}

void NFPlayerBase::IncreaseLevel()
{
    m_stRoleBase.level++;
    _has_bits_.SetBit(proto_ff::EN_ROLE_ATTR_LEVEL);
    MarkDirty();

    proto_ff::EventPlayerLevelUp levelUp;
    levelUp.set_level(m_stRoleBase.level);
    FindModule<NFIEventModule>()->FireExecute(NF_ST_LOGIC_SERVER, EVENT_PLAYER_LEVEL_UP, 0, m_stRoleBase.role_id, levelUp);
}

void NFPlayerBase::SetLevel(uint16_t iLevel)
{
    m_stRoleBase.level = iLevel;
    _has_bits_.SetBit(proto_ff::EN_ROLE_ATTR_LEVEL);
    MarkDirty();

    proto_ff::EventPlayerLevelUp levelUp;
    levelUp.set_level(m_stRoleBase.level);
    FindModule<NFIEventModule>()->FireExecute(NF_ST_LOGIC_SERVER, EVENT_PLAYER_LEVEL_UP, 0, m_stRoleBase.role_id, levelUp);
}

void NFPlayerBase::SetCE(int iCE)
{
    m_stRoleBase.role_ce = iCE;
    _has_bits_.SetBit(proto_ff::EN_ROLE_ATTR_CE);
    MarkDirty();
}

void NFPlayerBase::SetFirstRegOpenTime(int time)
{
    m_stRoleBase.first_reg_time = time;
    MarkDirty();
}

int NFPlayerBase::ClearAttrChangeFlag()
{
    _has_bits_.ClearAllBits();
    return 0;
}

int NFPlayerBase::MakeAttrCS(proto_ff::Proto_CSRoleAttrList* pRoleAttrList, bool bAll) const
{
    CHECK_NULL(0, pRoleAttrList);

    int iAttrCount = 0;
    if (bAll || _has_bits_.IsBitSetted(proto_ff::EN_ROLE_ATTR_LEVEL))
    {
        proto_ff::CSRoleAttr* pRoleAttr = pRoleAttrList->add_attr_list();
        pRoleAttr->set_type(proto_ff::EN_ROLE_ATTR_LEVEL);
        pRoleAttr->set_value(m_stRoleBase.level);
        iAttrCount++;
    }

    if (bAll || _has_bits_.IsBitSetted(proto_ff::EN_ROLE_ATTR_EXP))
    {
        proto_ff::CSRoleAttr* pRoleAttr = pRoleAttrList->add_attr_list();
        pRoleAttr->set_type(proto_ff::EN_ROLE_ATTR_EXP);
        pRoleAttr->set_value(m_stRoleBase.exp);
        iAttrCount++;
    }

    if (bAll || _has_bits_.IsBitSetted(proto_ff::EN_ROLE_ATTR_CE))
    {
        proto_ff::CSRoleAttr* pRoleAttr = pRoleAttrList->add_attr_list();
        pRoleAttr->set_type(proto_ff::EN_ROLE_ATTR_CE);
        pRoleAttr->set_value(m_stRoleBase.role_ce);
        iAttrCount++;
    }

    return iAttrCount;
}

int NFPlayerBase::MakePlayerDataCS(proto_ff::Proto_CSGetRoleRsp* pCSGetRoleRsp)
{
    m_stRoleBase.ToPb(pCSGetRoleRsp->mutable_role_base());
    return 0;
}

int NFPlayerBase::MakeCityPlayerData(proto_ff::CityRoleInfo* pGCCityRoleInfo)
{
    CHECK_NULL(0, pGCCityRoleInfo);
    pGCCityRoleInfo->set_role_id(m_stRoleBase.role_id);
    m_stRoleBase.ToPb(pGCCityRoleInfo->mutable_base());
    return 0;
}

int NFPlayerBase::MakeRoleCache(proto_ff::RoleOne* pRoleOne)
{
    m_stRoleCache.ToPb(pRoleOne);
    return 0;
}
